package algorithm;

import util.ListNode;

/**
 * 反转链表
 * @author rongkai
 * @description:
 * @date 2021/3/27
 */
public class Solution191 {
    public static ListNode reverseList(ListNode head) {
        if(head == null){
            return head;
        }
        ListNode node = null;
        while(true){
            ListNode next = head.next;
            head.next = node;
            node = head;
            if (next == null){
                break;
            }
            head = next;
        }
        return head;
    }

    public static void main(String[] args) {
        int[] nums ={1,2,3,4,5};
        ListNode node = ListNode.getListNode(nums);
        ListNode node1 = reverseList(node);
        while (node1 != null){
            System.out.println(node1.val);
            node1 = node1.next;
        }
    }
}
